package maurice.controller;

import wiiusej.*;
import wiiusej.wiiusejevents.physicalevents.*;
import wiiusej.wiiusejevents.utils.*;
import wiiusej.wiiusejevents.wiiuseapievents.*;

public class WiiController extends Controller implements WiimoteListener{
	private Wiimote wiimote;
	
	public WiiController(Wiimote wiimote){
		this.wiimote = wiimote;
		wiimote.activateMotionSensing();
		wiimote.addWiiMoteEventListeners(this);
		direction = Direction.NONE;
	}

	@Override
	public void onButtonsEvent(WiimoteButtonsEvent arg0) {
		if (arg0.isButtonAPressed()) {
			
		}
		
		if (arg0.isButtonBJustReleased()){
			//fireMovementConfirmed();
		}
	}

	@Override
	public void onClassicControllerInsertedEvent(
			ClassicControllerInsertedEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClassicControllerRemovedEvent(
			ClassicControllerRemovedEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDisconnectionEvent(DisconnectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onExpansionEvent(ExpansionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onIrEvent(IREvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMotionSensingEvent(MotionSensingEvent arg0) {
		if (arg0.getOrientation().getAPitch() > 50 && arg0.getOrientation().getAPitch() < 120) {
			direction = Direction.DOWN;
			fireMovementConfirmed();
		} else if (arg0.getOrientation().getAPitch() < -75 && arg0.getOrientation().getAPitch() > -120) {
			direction = Direction.UP;
			fireMovementConfirmed();
		} else {
			direction = Direction.ASIDE;
			fireMovementConfirmed();
		}
		
	}

	@Override
	public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusEvent(StatusEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void close(){
		super.close();
		wiimote.disconnect();
	}
}
